<template>
<div class="editor-page">
  <div class="container page">
    <div class="row">
      <div class="col-md-10 offset-md-1 col-xs-12">
        <ul class="error-messages">
          <li>That title is required</li>
        </ul>

        <form @submit.prevent="HandleAricle">
          <fieldset>
            <fieldset class="form-group">
              <input v-model="aricleInfo.article.title" type="text" class="form-control form-control-lg" placeholder="Article Title" />
            </fieldset>
            <fieldset class="form-group">
              <input v-model="aricleInfo.article.description" type="text" class="form-control" placeholder="What's this article about?" />
            </fieldset>
            <fieldset class="form-group">
              <textarea
              v-model="aricleInfo.article.body"
                class="form-control"
                rows="8"
                placeholder="Write your article (in markdown)"
              ></textarea>
            </fieldset>
            <fieldset class="form-group">
              <input type="text" class="form-control" placeholder="Enter tags" />
              <div class="tag-list">
                <span class="tag-default tag-pill"> <i class="ion-close-round"></i> tag </span>
              </div>
            </fieldset>
            <input class="btn btn-lg pull-xs-right btn-primary" type="submit">
              Publish Article
            </input>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
</template>
<script setup lang="ts">
import { CreateArticleInfo, ArticleDetail } from '@/model';
import { ref } from 'vue';
import { createAricle, getArticle, update_article } from '@/api';
import { useRouter, useRoute } from 'vue-router';

const router = useRouter()
let aricleInfo = ref<CreateArticleInfo>({
  article: {
    title: '',
    description: '',
    body: '',
    tagList: []
  }
})

function HandleAricle(){
  if(route.params.article_id != undefined){
    update_article(aricleInfo.value,route.params.article_id as string).then(res =>{
        console.log(res.data);
        router.push({name:'article', params:{article_id:res.data.article.slug}})
    }).catch(err=>{
      console.log(err);
    })
  }else{
     createAricle(aricleInfo.value).then(res =>{
        console.log(res.data);
        router.push({name:'article', params:{article_id:res.data.article.slug}})
        
    }).catch(err=>{
      console.log(err);
    })
  }
}

const route = useRoute()

const my_article = ref<ArticleDetail>({
    article:{
        slug: '',
        title: '',
        description: '',
        tagList: [],
        createdAt: '',
        updatedAt: '',
        favorited: false,
        favoritesCount: 0,
        author: {
            username: '',
            bio: '',
            image: '',
            following: false
        }
    }
});

if(route.params.article_id != undefined){
  getArticle(route.params.article_id as string).then(res =>{
    my_article.value = res.data
    aricleInfo.value.article.title = my_article.value.article.title
    aricleInfo.value.article.description = my_article.value.article.description
    aricleInfo.value.article.body = my_article.value.article.body!
})
}

</script>
<style scoped>
</style>
